﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;

namespace Portal.Systems.Render
{
    public class GroupRoleRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        SortIndex = document.getElementById('txtSortIndex').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.GroupRoleRenderBll.ServerSideReading(SiteId, SortIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListGroupRole').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +


                   "    function ClientAdd()\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.GroupRoleRenderBll.DrawAddForm(SiteId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtGroupRoleName').focus();\r\n"+
                   "    }\r\n" +

                   "    function ClientUpdate(GroupRoleId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.GroupRoleRenderBll.DrawUpdateForm(SiteId, GroupRoleId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtGroupRoleName').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientInputData(GroupRoleId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "    }\r\n" +


                   "    function ClientBack()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divActionForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientAddGroupRole()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        GroupRoleName = document.getElementById('txtGroupRoleName').value;\r\n" +
                   "        SortIndex = document.getElementById('txtSortIndex').value;\r\n"+
                   "        AjaxOut = Portal.Systems.Render.GroupRoleRenderBll.ServerSideAdd(SiteId, GroupRoleName, SortIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n"+
                   "        {\r\n"+
                   "            alert(AjaxOut.InfoMessage);\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListGroupRole').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n"+
                   "    }\r\n" +


                   "    function ClientDelete(GroupRoleId)\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n"+
                   "        SortIndex = document.getElementById('txtSortIndex').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.GroupRoleRenderBll.ServerSideDelete(SiteId, GroupRoleId, SortIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListGroupRole').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+

                   "    function ClientSaveGroupRole(GroupRoleId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        GroupRoleName = document.getElementById('txtGroupRoleName').value;\r\n" +
                   "        SortIndex = document.getElementById('txtSortIndex').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.GroupRoleRenderBll.ServerSideSave(SiteId,GroupRoleId, GroupRoleName, SortIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListGroupRole').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListGroupRole').style.display = 'block';\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n"+
                   "</script>\r\n" +
                   "<div id=divActionForm></div>\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\""+ButtonIconRoot+"/Add.png\"><div>Tạo mới</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "    <div class=tabcontent id=\"divListGroupRole\">\r\n" +
                            ServerSideReading(SiteId).HtmlContent+
                   "    </div>\r\n"+
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>"+ ex.Message.ToString()+"</div>\r\n";
            }
            
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                GroupRoleCls[] GroupRole = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingGroupRoles(SiteId, null);
                if (GroupRole.Length == 0)
                {
                    OAjaxOut.HtmlContent = "<div class=CssNoData>Chưa có nhóm nào trong hệ thống</div>\r\n";
                    return OAjaxOut;
                }
                string Html =
                   "        <h3>Danh sách nhóm</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th>Nhóm truy cập</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < GroupRole.Length; iIndex++)
                {
                    string UpdateButton = "<input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + GroupRole[iIndex].GroupRoleId + "');\">";
                    string DeleteButton = "<input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + GroupRole[iIndex].GroupRoleId + "');\">";
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "        <td>" + GroupRole[iIndex].GroupRoleName + "</td>\r\n" +
                       "        <td style=\"width:10px;\">"+UpdateButton+"</td>\r\n" +
                       "        <td style=\"width:10px;\">"+DeleteButton+"</td>\r\n" +
                       "        <td style=\"width:10px;\"><input type=button class=button value=\"Nhóm\" onclick=\"window.open('RoleInGroupRole.aspx?GroupRoleId="+GroupRole[iIndex].GroupRoleId+"','_self');\"></td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddGroupRole();\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "<div class=CssFormSaveUpdate>\r\n"+
                   "  <h3>Thêm mới nhóm</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Đối tượng:</td>\r\n" +
                   "        <td class=Col2><input id=txtGroupRoleName class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n"+
                   "    <tr>\r\n" +
                   "        <td class=Col1>Thứ tự:</td>\r\n" +
                   "        <td class=Col2><input id=txtSortIndex type=textbox class=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId, string GroupRoleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
            
                GroupRoleCls OGroupRole = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateGroupRoleModel(SiteId, GroupRoleId);
                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSaveGroupRole('"+GroupRoleId+"');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n"+
                   "  <h3>Sửa nhóm truy cập</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tên nhóm:</td>\r\n" +
                   "        <td class=Col2><input id=txtGroupRoleName class=textbox type=textbox value=\"" + OGroupRole.GroupRoleName + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Thứ tự:</td>\r\n" +
                   "        <td class=Col2><input id=txtSortIndex class=textbox type=textbox value=\"" + OGroupRole.SortIndex + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(
            string SiteId,
            string GroupRoleName, 
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(GroupRoleName)) throw new Exception("Chưa nhập tên nhóm!");
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddGroupRole(null, SiteId, AssetCode, null, GroupRoleName, int.Parse(SortIndex));
                OAjaxOut.HtmlContent = ServerSideReading(SiteId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(
            string SiteId, 
            string GroupRoleId, 
            string GroupRoleName, 
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(GroupRoleName)) throw new Exception("Chưa nhập tên truy cập!");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveGroupRole(null, SiteId, GroupRoleId,GroupRoleName, int.Parse(SortIndex));
                OAjaxOut.HtmlContent = ServerSideReading(SiteId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(
            string SiteId, 
            string GroupRoleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeleteGroupRole(null, SiteId, GroupRoleId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
